unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const symb=['a'..'z'];
      num= ['1'..'9'];

var
  Form1: TForm1;
  int: array ['a'..'z'] of integer;

implementation

{$R *.dfm}

function StrCalc(exp:string):string; external 'StrMatch.dll';

function FindVariable(s:string):string;
var i: integer;
    b: boolean;
begin
 result:='';
 b:=false;
 for i:=1 to length(s) do begin
   if s[i] in symb then begin
     if b=false then b:=not b;
     result:=result+s[i];
   end else if b=true then exit;
 end;
end;

procedure ReplaceCode(var s:string);
var i:integer;
    s1:string;
begin
  while findvariable(s)<>'' do begin
    i:=pos(findvariable(s), s);
    s1:=findvariable(s);
    delete(s, i, length(s1));
    insert(inttostr(int[s1[1]]), s, i);
  end;
end;

function NormalReal(s:string):string;
var i:integer;
begin
 i:=pos('.', s);
 if i=0 then exit;
 result:=s;
 delete(result, i, 1);
 insert(',', result, i);
end;

procedure Assignment(variable,value:string);
var i:integer;
    b: boolean;
    s1:string;
begin
  b:=true;
  for i:=1 to length(value) do
   if not (value[i] in symb) then b:=false;
  if b=true then begin int[variable[1]]:=int[value[1]]; exit; end;
  b:=true;
  for i:=1 to length(value) do
   if not (value[i] in num) then b:=false;
  if b=true then begin int[variable[1]]:=strtoint(value); {showmessage(value);} exit; end;
  s1:=value;
  //showmessage(s1);
  replacecode(s1);
  int[variable[1]]:=trunc(strtofloat(normalreal(strcalc(s1))));
end;

procedure TForm1.Button1Click(Sender: TObject);
var s:string;
    i:integer;
begin
 {int['a']:=1;
 int['b']:=3;
 s:=memo1.lines[0];
 replacecode(s);
 memo1.Lines.Add(s);}
 memo1.Lines.Add('=====Console=====');
 for i:=0 to memo1.lines.count-2 do begin
   if copy(memo1.Lines[i], 1, 5)='write' then memo1.Lines.Add(inttostr(int[memo1.Lines[i][7]]));
  // memo1.Lines.Add(findvariable(memo1.Lines[i]));
   //memo1.Lines.Add(copy(memo1.Lines[i], length(findvariable(memo1.Lines[i]))+3, length(memo1.Lines[i])-length(findvariable(memo1.Lines[i]))-2));
   if copy(memo1.Lines[i],length(findvariable(memo1.Lines[i]))+1,2)=':=' then Assignment(findvariable(memo1.Lines[i]), copy(memo1.Lines[i], length(findvariable(memo1.Lines[i]))+3, length(memo1.Lines[i])-length(findvariable(memo1.Lines[i]))-2))
 end;
end;

end.
